global class copyaccount {
   webService static String amethod(Id a){
    	opportunity o = [Select id,Accountid from opportunity where id =:a];
    	
    	Schema.DescribeSObjectResult solTemp = Solution__c.SObjectType.getDescribe(); 
        Map<String, Schema.SObjectField> allFieldsMap = solTemp.fields.getMap();

        String SOQLSelect = '';
        
        for(String fldName : allFieldsMap.keySet()){
            Schema.SObjectField fld = allFieldsMap.get(fldName);
            DescribeFieldResult fieldResult = fld.getDescribe();
            string fieldName = fieldResult.getName();
            System.debug('Got field:'+fieldName);
            if(SOQLSelect != ''){
                SOQLSelect += ',' + fieldName;
            }
            else{
                SOQLSelect += 'select ' + fieldName;
            }     
        }             	
		String SOQLStmt =SOQLSelect + ' from Solution__c where opportunity__r.id =' + '\'' + a + '\'';        
		List<Solution__c> soln = Database.query(SOQLStmt);
		
		List<Solution__c> accntSolnList = new List<Solution__c>();
    	
    	try{

    		
    		Schema.DescribeSObjectResult solCompTemp = SolutionComponent__c.SObjectType.getDescribe(); 
       		allFieldsMap = solCompTemp.fields.getMap();

        	SOQLSelect = '';
        
	        for(String fldName : allFieldsMap.keySet()){
	            Schema.SObjectField fld = allFieldsMap.get(fldName);
	            DescribeFieldResult fieldResult = fld.getDescribe();
	            string fieldName = fieldResult.getName();
	            System.debug('Got field:'+fieldName);
	            if(SOQLSelect != ''){
	                SOQLSelect += ',' + fieldName;
	            }
	            else{
	                SOQLSelect += 'select ' + fieldName;
	            }     
	        } 
			

	        SOQLStmt =SOQLSelect + ' from SolutionComponent__c where Solution__r.id in' + '(';  
    		
    		DummyOptyHolder__c optyCustomSetting = DummyOptyHolder__c.getInstance();
    		string DumyOptyId = optyCustomSetting.OptyId__c;
    		Map<Id,Id> keyMap = new Map<Id, Id>();
	    	Boolean isFirst = true;
	    	for(Solution__c sol: soln){
	    		Solution__c accntSol = sol.Clone(false, true);
	        	accntSol.Account__c = o.Accountid;
	        	accntSol.IsAccountSoltuion__c = true;
	        	//accntSol.Opportunity__c = '006A000000EfKnu';
	        	accntSol.Opportunity__c = DumyOptyId;
	        	accntSolnList.add(accntSol) ;
	        	if(isFirst){
	        		SOQLStmt += '\'' + sol.Id + '\'';
	        		isFirst= false;
	        	}
	        	else{	        	
	        		SOQLStmt += ',\'' + sol.Id + '\'';
	        	}
	     	}
	     	
	     	SOQLStmt += ')';
	     		     	
	     	insert(accntSolnList);
	     	
	     	Integer i = 0;
	     	for(Solution__c accntSol : accntSolnList)
	     	{
	     		keyMap.put(soln.get(i).Id, accntSol.Id); 
	     		i++;
	     	}
	     	
	     	system.debug('SOQL Stmt: ' + SOQLStmt);
	     	List<SolutionComponent__c> solCompList = Database.query(SOQLStmt);
    		List<SolutionComponent__c> accntsolCompList = new List<SolutionComponent__c>();
	     	
	     	for(SolutionComponent__c solComp :solCompList){
	     		system.debug('Solution Component' + solComp.Component__c);
	     		SolutionComponent__c accntSolComp = solComp.Clone(false, true);
	     		accntSolComp.Solution__c = keyMap.get(solComp.Solution__c);
	     		system.debug('Solution Id Map' + keyMap.get(solComp.Solution__c));
	     		accntsolCompList.add(accntSolComp);
	     	}
	     	insert(accntsolCompList);
     	}
     	
     	
	    catch(Exception e){
	    	system.debug('EXCEPTION OCCURED' + e);
	    	return e.getMessage();
	    }
	    
	    return 'Sucess';
	}
}